/**
 * 
 */
package com.ternnetwork.core.service.impl;

import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;


import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.ternnetwork.persistence.page.BootstrapGrid;
import com.ternnetwork.persistence.page.Page;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.core.dao.RescRoleDao;
import com.ternnetwork.core.dao.RoleDao;
import com.ternnetwork.core.dao.UserRoleDao;
import com.ternnetwork.core.entity.RescRole;
import com.ternnetwork.core.entity.Role;
import com.ternnetwork.core.entity.UserRole;
import com.ternnetwork.core.service.RoleService;








/**
 *
 * @author wenfeng.xu
 *2011-4-24下午09:33:05
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
	@Resource
	private RoleDao roleDao;
	
	@Resource
	private RescRoleDao  rescRoleDao;
	
	@Resource
	private UserRoleDao  userRoleDao;
	@Override
	public BaseResponse idoAdd(Role t) {
		
		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getNameZh())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空！");
			return res;
		}
	
       if(StringUtils.isEmpty(t.getNameEn())){
			res.setStatus(0);
			res.setMessage("操作失败，代码不能为空！");
			return res;
		}
       
       long totalCount=roleDao.getTotalCount("select count(t.id) from Role t where t.nameEn=?1", t.getNameEn());
	   if(totalCount>0L){
		   res.setStatus(0);
			res.setMessage("操作失败，代码已经存在！");
			return res;
	   }	
	   roleDao.persist(t);
	   res.setStatus(1);
	   res.setMessage("操作成功！");
       return res;
	}
	@Override
	public BaseResponse idoUpdate(Role t) {
		
		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getNameZh())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空！");
			return res;
		}
	
       if(StringUtils.isEmpty(t.getNameEn())){
			res.setStatus(0);
			res.setMessage("操作失败，代码不能为空！");
			return res;
		}
		Role oldRole=findById(t.getId());
		
		if(oldRole==null){
			   res.setStatus(0);
			   res.setMessage("操作失败，对象不存在！");
			   return res;
		}
        long totalCount=roleDao.getTotalCount("select count(t.id) from Role t where t.nameEn=?1 and t.id!=?2", t.getNameEn(),t.getId());
        if(totalCount>0L){
			   res.setStatus(0);
			   res.setMessage("操作失败，代码已经存在！");
			   return res;
		}	
        oldRole.setCategory(t.getCategory());
        oldRole.setDescription(t.getDescription());
        oldRole.setNameEn(t.getNameEn());
        oldRole.setNameZh(t.getNameZh());
      
		roleDao.saveOrUpdate(oldRole);
		res.setStatus(1);
		res.setMessage("操作成功！");
	    return res;
	}

	
	@Override
	public List<Role> findAll(String jpql, Object... param) {
		return roleDao.findAll(jpql, param);
	}
	@Override
	public Role findById(long id) {
		return roleDao.findById(Role.class, id);
	}
	@Override
	public Role getReferenceById(long id) {
		return roleDao.getReferenceById(Role.class, id);
	}
	@Override
	public Long getTotalCount(String countJpql, Object... param) {
		return roleDao.getTotalCount(countJpql, param);
	}
	@Override
	public String query(Page page){
		String jpql="from Role t order by id asc";
		Page<Role> result=roleDao.query("select count(t.id) from Role t",jpql, page,null);
		BootstrapGrid grid=new BootstrapGrid(result);
		return grid.toString();
	}
	@Override
	public List<Role> findAllByCategory(String category) {
		return roleDao.findAll("from Role where category=?1", category);
	}
	@Override
    public Role findByCode(String code ){
    	List<Role> list=roleDao.findAll("from Role where nameEn=?1",code);
    	return list.size()==0?null:list.get(0);
    }

	@Override
	public BaseResponse idoDelete(long id) {
		BaseResponse res=new BaseResponse();
   		Role oldRole=findById(id);
		if(oldRole==null){
			   res.setStatus(0);
			   res.setMessage("操作失败，对象不存在！");
			   return res;
		}
		userRoleDao.bulkUpdate("delete from UserRole where role.id=?1", oldRole.getId());
		rescRoleDao.bulkUpdate("delete from RescRole where role.id=?1", oldRole.getId());
		roleDao.delete(oldRole);
	    res.setStatus(1);
	    res.setMessage("操作成功！");
		return res;
	}
}
